/**
 * 
 */
package com.sunnos.express.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunnos.express.entity.Right;
import com.sunnos.express.entity.User;
import com.sunnos.express.mapper.UserMapper;

/**
 * @author lf
 *
 */
@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;
	
	/**
	 * ��ȡĳ���û��˵���Ϣ
	 * @param param �û���ɫID
	 * @return
	 */
	public List<Right> getUserMenus(int param) {
		Map<String,Object> map = new HashMap<String,Object>();
		List<Right> menus = userMapper.getTopMenusByRoleId(param);
		Iterator<Right> it = menus.iterator();
		while (it.hasNext()) {
			Right right = it.next();
			right.setIsLeaf(false);
			map.put("parentId", right.getId());
			map.put("roleId", param);
			right.setChildNode(userMapper.getSubMenus(map));
		}			
		return menus;
	}
	
	/**
	 * ��ȡĳ���û�Ȩ����Ϣ
	 * @param param �û���ɫID
	 * @return
	 */
	public List<Right> getUserRights(int param) {
		return userMapper.getUserRights(param);
	}
	
	/**
	 * ��ȡĳ���û�
	 * @param param
	 * @return
	 */
	public User getUser(Map<String,Object> param) {
		return userMapper.getUser(param);
	}
	/**
	 * ��ȡ�û��б�
	 * @return
	 */
	public List<User> getUserList(Map<String,Object> param) {
		return userMapper.getUserList(param);
	}
	
	public int getUserListTotal(Map<String,Object> param) {
		return userMapper.getUserListTotal(param);
	}
	
	/**
	 * �����û�
	 * @param user
	 */
	@Transactional
	public void insertUser(User user) {
		//�����û�
		userMapper.insertUser(user);
	}
	/**
	 * �༭�û�
	 * @param user
	 */
	@Transactional
	public void updateUser(User user) {
		//�༭�û�
		userMapper.updateUser(user);
	}
	/**
	 * ɾ���û�
	 * @param param
	 */
	@Transactional
	public void deleteUser(String ids) {
		String[] idsArray = ids.split(",");
		for(int i=0;i<idsArray.length;i++) {			
			userMapper.deleteUser(idsArray[i]);
		}
	}
	/**
	 * �޸�����
	 * @param param
	 */
	@Transactional
	public void changeUserPassword(Map<String,Object> param) {
		userMapper.changeUserPassword(param);
	}
}
